﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_Organization")]
    public class T_Organization : IEntity<T_Organization>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 行业投稿代码
        /// 石油与天然气工业：oilandgas
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string IndustryCode { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string IndustryName { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Organization { get; set; }

        public override T_Organization Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Organization Find(System.Linq.Expressions.Expression<Func<T_Organization, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Organization.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.IndustryName = item.IndustryName;
                    this.Organization = item.Organization;
                    this.IndustryCode = item.IndustryCode;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Organization, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Organization.Any(where);
            }
        }
    }
}
